import requests

from api import log
from api.api_approve_trust import ApiApproveTrust
from api.api_register_login import ApiRegisterLogin
from tool import common_assert, html_parser


class TestApproveTrust:
    # 初始化
    def setup(self):
        # 获取session对象
        self.session = requests.session()
        # 获取ApiApproveTrust对象
        self.api = ApiApproveTrust(self.session)
        # 获取ApiRegisterLogin对象并调用登录
        r = ApiRegisterLogin(self.session).api_login()
        print(r.text)

    # 结束
    def teardown(self):
        self.session.close()

    # 1、认证接口 测试
    def test01_approve(self,expect_json="提交成功"):
        try:
            # 调用认证接口
            result = self.api.api_approve()
            log.info("认证接口执行结果为：{}".format(result.text))
            common_assert(result,expect_json=expect_json)
            log.info("认证接口断言通过！")
        except Exception as e:
            log.error(e)
            raise

    # 2、查询认证状态接口 测试
    def test02_approve_status(self,expect_text="1111"):
        try:
            # 调用认证接口
            result = self.api.api_approve_status()
            log.info("查询认证状态接口执行结果为：{}".format(result.text))
            common_assert(result, expect_text=expect_text)
            log.info("查询认证状态接口断言通过！")
        except Exception as e:
            log.error(e)
            raise

    # 3、后台开户接口 测试
    def test03_trust(self,expect_text="form",expect_text2="OK"):
        try:
            # 调用认证接口
            result = self.api.api_trust()
            print("后台开户结果为：{}".format(result.json()))
            log.info("后台开户接口执行结果为：{}".format(result.json()))
            common_assert(result, expect_text=expect_text)
            log.info("后台开户接口断言通过！")
            # 三方开户如何实现
            data = html_parser(result)
            result = self.session.post(url=data[0],data=data[1])
            common_assert(result, expect_text=expect_text2)
            log.info("三方开户接口执行结果为：{}".format(result.text))
        except Exception as e:
            log.error(e)
            raise
